// miniprogram/pages/playlist/playlist.js
const MAX_LIMIT = 15
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperImgUrls: [{
      url: 'https://p2.music.126.net/MWmPF_gvNgLoaMUC246ZCQ==/109951164534246836.jpg'
    }, {
      url: 'https://p2.music.126.net/VFd5cboNTbnYsWZ5DBn9bg==/18953381440004340.jpg'
    }, {
      url: 'https://p2.music.126.net/em_fXgUJDApoV_3_8HYkNg==/109951164362973668.jpg'
    }],
    playlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getPlayList()
  },

  onBannerTap () {
    wx.vibrateLong({
      success (res) {
      },
      fail (res) {
      }
    })
    wx.showModal({
      title: '吃惊不',
      content: '除了手机震了一震，\r\n好像没啥其他效果啊~',
      showCancel: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this._getPlayList('refresh')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this._getPlayList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  _getPlayList (type = 'load') {
    wx.showLoading({
      title: '加载中'
    })
    wx.cloud.callFunction({
      name: 'music',
      data: {
        $url: 'playlist',
        start: this.data.playlist.length,
        count: MAX_LIMIT
      }
    }).then(res => {
      if (type === 'refresh') {
        this.setData({
          playlist: this.data.playlist
        })
      }
      this.setData({
        playlist: this.data.playlist.concat(res.result.data)
      })
      wx.hideLoading()
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  }
})